﻿using System.Windows;
using System.Windows.Media;
using System.Globalization;

namespace BaseTypesControlLib.Controls
{
    //implements ieditable and ireadonly
    public class EditFieldControl:FieldControl
    {
        public EditFieldControl()
        {
            DefaultStyleKey = typeof(EditFieldControl);
        }

        protected override Size MeasureOverride(Size constraint)
        {
            
            string lbl = Label;
            string txt = Text;
            if (lbl == null) lbl = "";
            if (txt == null) txt = "";

            string[] lines = txt.Split(new char[] { ' ', '\n' });
            string stringToCheck = lbl + lines[0];
            FormattedText ft = new FormattedText(stringToCheck, CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight
                , new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, Brushes.Black);
            if (ft.Width > constraint.Width)
            {
                txt = lbl + "\n" + txt;
            }
            else
            {
                txt = lbl + txt;
            }
            DisplayText = txt;

            Size sz = base.MeasureOverride(constraint);
            sz.Width = constraint.Width;

            return sz;
        }
        static EditFieldControl()
        {
            FrameworkPropertyMetadata fpm = new FrameworkPropertyMetadata(string.Empty,
                FrameworkPropertyMetadataOptions.AffectsMeasure |
                FrameworkPropertyMetadataOptions.AffectsArrange);
            LabelProperty = DependencyProperty.Register("Label", typeof(string), 
                typeof(EditFieldControl), fpm);

            fpm = new FrameworkPropertyMetadata(string.Empty,
                FrameworkPropertyMetadataOptions.AffectsMeasure |
                FrameworkPropertyMetadataOptions.AffectsArrange);
            TextProperty = DependencyProperty.Register("Text", typeof(string), 
                typeof(EditFieldControl), fpm);

            fpm = new FrameworkPropertyMetadata(string.Empty, 
                FrameworkPropertyMetadataOptions.AffectsMeasure|
                FrameworkPropertyMetadataOptions.AffectsArrange);
            DisplayTextProperty = DependencyProperty.Register("DisplayText", typeof(string), 
                typeof(EditFieldControl), fpm);
        }

        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        public static readonly DependencyProperty LabelProperty;

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty;

        public string DisplayText
        {
            get { return (string)GetValue(DisplayTextProperty); }
            set { SetValue(DisplayTextProperty, value); }
        }

        public static readonly DependencyProperty DisplayTextProperty;
    }
}
